#!/usr/bin/python3
 
import sys
import os
import re
import argparse
 
def gen_align_log(tmp_line):
    if len(tmp_line) == 0:
        return tmp_line
 
    hash    = []
    cnt     = 1
    out_line = []
 
    for line in tmp_line:
        i = 0
        line = line.strip()
        if len(hash) == 0:
            cnt = line.count("|") + 1
            for j in range(0, cnt):
                hash.append(0)
        for sub in line.split("|"):
            sub = sub.strip()
            hash[i] = max(len(sub), hash[i])
            i = i + 1
    for line in tmp_line:
        i = 0
        str = ""
        for sub in line.split("|"):
            sub  = sub.strip()
            str1 = "{:<%0d}" % hash[i]
            if i == cnt - 1:
                str = str + str1.format(sub)
                out_line.append(str)
            else:
                str = str + str1.format(sub) + " | "
            i = i + 1
    
    return out_line
 
def main():
    output_file = []
    tmp_line    = []
 
    input_file = sys.argv[1]
    with open (input_file, "r") as fl:
        handle = fl.readlines()
        pre_cnt = 0
        for line in handle:
            line = line.strip()
            if line.count("|") > 1:
                if len(tmp_line) == 0:
                    tmp_line.append(line)
                    pre_cnt = line.count("|")
                elif line.count("|") == pre_cnt:
                    tmp_line.append(line)
                else:
                    output_file.extend(gen_align_log(tmp_line))
                    tmp_line = []
                    tmp_line.append(line)
                    pre_cnt = line.count("|")
 
            else:
                output_file.extend(gen_align_log(tmp_line))
                tmp_line = []
                pre_cnt = 0
                output_file.append(line)
 
        if len(tmp_line) > 0:
            output_file.extend(gen_align_log(tmp_line))
 
    with open (input_file + ".align.log", "w") as fl:
        for line in output_file:
            fl.write(line + "\n")
 
 
if __name__ == '__main__':
    main()
